package com.cswangdao.controller.admin;

import com.cswangdao.domain.dto.BrandPageDTO;
import com.cswangdao.domain.model.MarketBrand;
import com.cswangdao.result.PageResult;
import com.cswangdao.result.Result;
import com.cswangdao.service.BrandService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.IOException;


@RestController
@RequestMapping("/admin/brand")
@Slf4j
public class AdminBrandController {

    @Autowired
    private BrandService brandService;

    /**
     * @description: 品牌信息分页查询
     * @param: request，response
     * @return: void
     */
    @GetMapping("/list")
    public Result<PageResult<MarketBrand>> list(BrandPageDTO pageDTO) {
        log.info("品牌信息分页查询:{}", pageDTO);
        PageResult<MarketBrand> pageResult = brandService.pageQueryInfo(pageDTO);
        return Result.success(pageResult);

    }

    /**
     * @description: 新增品牌信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/create")
    public Result<MarketBrand> create(@RequestBody MarketBrand brand) {
        log.info("新增品牌信息:{}", brand);
        MarketBrand res = brandService.addBrand(brand);
        return Result.success(res);
    }

    /**
     * @description: 修改品牌信息
     * @param: request, response
     * @return: void
     */

    @PostMapping("/update")
    public Result update(@RequestBody MarketBrand brand) throws IOException {
        boolean flag = brandService.updateById(brand);
        return flag ? Result.success() : Result.error("修改失败");
    }

    /**
     * @description: 删除品牌信息
     * @param: request, resopnse
     * @return: void
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody MarketBrand brand) throws IOException {
        log.info("删除品牌信息:{}", brand);
        boolean flag = brandService.deleteById(brand.getId());
        return flag ? Result.success() : Result.error("删除失败");
    }
}
